
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include "chat.h"


gboolean on_chatwin_close(GtkWidget *widget, GdkEvent  *event, gpointer user_data)
{
	g_printf("chat window closing...\n");
	gtk_widget_destroy(widget);
	gtk_main_quit();
	return FALSE;
}
void on_send_clicked(GtkButton *button, gpointer   user_data)
{
	GtkTextIter start, end;
	ChatWinRef *widgets = (ChatWinRef *)user_data;
	GtkWidget *textInputView = widgets -> textInputView;
	GtkTextBuffer *inputBuf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textInputView));
	MyMsgUser *user = widgets -> destUser;
	gtk_text_buffer_get_bounds(inputBuf, &start, &end);
	gchar *msg = gtk_text_buffer_get_text(inputBuf, &start, &end,FALSE);
	g_printf("sendMsg:%s to %s\n", msg, user->ipAddr);
	
	//g_free(user_data); // 释放内存
}

void on_close_clicked(GtkButton *button, gpointer   user_data)
{
	ChatWinRef *widgets = (ChatWinRef *)user_data;
	GtkWidget *chatWin = widgets -> chatWin;
	gtk_widget_destroy(chatWin);
}
void on_fontsel_clicked(GtkButton *button, gpointer   user_data)
{

}
void on_colorsel_clicked(GtkButton *button, gpointer   user_data)
{

}
GtkWidget *createChatWindow(MyMsgUser *destUser)
{
	GtkWidget *chatWin;
	GtkWidget *mainvBox;
	GtkWidget *tophBox, *topLabel;
	GtkWidget *midhBox;
	GtkWidget *midLeftvBox;
	GtkWidget *midRightvBox, *midRightLabel;
	
	GtkWidget *textShowView, *textInputView;
	GtkWidget *scwinup, *scwindown;
	GtkTextBuffer *showBuf, *inputBuf;
	GtkWidget *vpaned;
	GtkWidget *inputvBox, *fontSelhBox, *sendBtnhBox;	
	GtkWidget *btnClose, *btnSend, *btnFont, *btnColor;
	
	chatWin = gtk_window_new(GTK_WINDOW_TOPLEVEL);	
	g_signal_connect(G_OBJECT(chatWin),"delete-event",G_CALLBACK(on_chatwin_close),NULL);  
	gtk_window_set_title(GTK_WINDOW(chatWin),"聊天窗口");  
	//设定窗口的默认宽高  
	gtk_window_set_default_size(GTK_WINDOW(chatWin),525,450);  
	//设定窗口的位置,让窗口总在最前端  
	gtk_window_set_position(GTK_WINDOW(chatWin),GTK_WIN_POS_CENTER);  
	//设定容器边框的宽度  
	gtk_container_set_border_width(GTK_CONTAINER(chatWin),2);

	mainvBox = gtk_vbox_new(FALSE,0);
	gtk_container_add(GTK_CONTAINER(chatWin), mainvBox);
	// 顶部
	tophBox = gtk_vbox_new(FALSE,0);
	gtk_box_pack_start(GTK_BOX(mainvBox), tophBox, FALSE, FALSE, 5);
	gtk_widget_set_size_request(tophBox, 500, 80);
	topLabel = gtk_label_new("头像和用户名等信息...");
	gtk_widget_set_size_request(topLabel, 500, 80);
	gtk_box_pack_start(GTK_BOX(tophBox), topLabel, FALSE, FALSE, 5);	
	
	// 中部
	midhBox = gtk_hbox_new(FALSE,0);
	gtk_box_pack_start(GTK_BOX(mainvBox), midhBox, TRUE, FALSE, 5);

	
	// 中左
	midLeftvBox = gtk_vbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(midhBox), midLeftvBox, TRUE, FALSE, 0);
	// 中右
	midRightvBox = gtk_vbox_new(FALSE, 0);
	//gtk_widget_set_size_request(midRightvBox, 100, 320);
	gtk_box_pack_start(GTK_BOX(midhBox), midRightvBox, FALSE, FALSE, 0);
	midRightLabel = gtk_label_new("对方用户信息");
	//gtk_widget_set_size_request(midRightLabel, 100, 320);
	gtk_box_pack_start(GTK_BOX(midRightvBox), midRightLabel, FALSE, FALSE, 0);
	
	
	vpaned = gtk_vpaned_new();
	gtk_box_pack_start(GTK_BOX(midLeftvBox), vpaned, TRUE, TRUE, 0);
	// 显示聊天记录部分
	scwinup = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwinup), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_paned_pack1(GTK_PANED(vpaned), scwinup, TRUE, FALSE);
	textShowView = gtk_text_view_new();
	gtk_container_add(GTK_CONTAINER(scwinup), textShowView);
	gtk_paned_set_position(GTK_PANED(vpaned), 200);

	// 输入消息部分
	inputvBox = gtk_vbox_new(FALSE, 0);
	gtk_paned_pack2(GTK_PANED(vpaned), inputvBox, TRUE, FALSE);
	fontSelhBox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(inputvBox), fontSelhBox, FALSE, FALSE, 0);	// 字体选择栏
	btnFont = gtk_button_new_with_label("选择字体");
	btnColor = gtk_button_new_with_label("选择颜色");
	gtk_box_pack_start(GTK_BOX(fontSelhBox), btnFont, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(fontSelhBox), btnColor, FALSE, FALSE, 0);
	scwindown = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwinup), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_box_pack_start(GTK_BOX(inputvBox), scwindown, TRUE, FALSE, 0);	// 输入框容器
	textInputView = gtk_text_view_new();
	gtk_container_add(GTK_CONTAINER(scwindown), textInputView);	// 输入框
	sendBtnhBox = gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(inputvBox), sendBtnhBox, FALSE, FALSE, 0);	// 按钮容器
	btnClose = gtk_button_new_with_label("关闭");
	btnSend = gtk_button_new_with_label("发送");
	gtk_box_pack_end(GTK_BOX(sendBtnhBox), btnSend, FALSE, FALSE, 0);	// 按钮
	gtk_box_pack_end(GTK_BOX(sendBtnhBox), btnClose, FALSE, FALSE, 0);	// 按钮
	//gtk_widget_set_size_request(btnSend, 80, 20);
	ChatWinRef *chatWinRefs = g_malloc(sizeof(ChatWinRef));
	chatWinRefs->chatWin = chatWin;
	chatWinRefs->textShowView = textShowView;
	chatWinRefs->textInputView = textInputView;
	chatWinRefs->destUser = destUser;
	// 绑定按钮事件监听
	//g_printf("#####ip=%s\n",destUser->ipAddr);
	g_signal_connect(G_OBJECT(btnSend),"clicked",G_CALLBACK(on_send_clicked), chatWinRefs);
	g_signal_connect(G_OBJECT(btnClose),"clicked",G_CALLBACK(on_close_clicked), chatWinRefs);
	gtk_widget_set_size_request(textShowView, 400, 200);
	gtk_widget_set_size_request(textInputView, 400, 150);
	return chatWin;
}
